package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Datautil {
	public static Connection conn;
	public static String DATA="jdbc:sqlserver://localhost;DATABASENAME=QUANLYDEANCONGTY";
	public static String USER_NAME="sa";
	public static String PASSWORD="123456";
	
	public static void connect() {
		try{
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		conn = DriverManager.getConnection(DATA, USER_NAME, PASSWORD);
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection(){
		if (conn == null) {
			connect();
		}
		return conn;
	}
		
	public static ResultSet executeQuery(String sql){
		try{
			connect();
			Statement statement =  conn.createStatement();
			ResultSet rs = statement.executeQuery(sql);
			return rs;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
		
	public static Boolean executeNonQuery(String sql){
		try{
			connect();
			Statement statement = conn.createStatement();
			statement.executeUpdate(sql);
			return true;
		}catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
}
